﻿<?php
require_once 'Constant.php' ;
class User_ClientController extends AppController
{
    public function listAction()
    {
        $objClientTable = new Cts_Db_Client() ;
        $aParams = $this->getRequest()->getParams() ;
        $nStart = isset( $aParams['start'] ) ? $aParams['start'] : 0 ;
        $nLimit = isset( $aParams['limit'] ) ? $aParams['limit'] : 0 ;
        
        try
        {   
            $aClients = $objClientTable->getList( '1=1' , $nStart , $nLimit ) ;
            
            //Zend_Debug::dump( $aUsers ) ;
            
            $nCountClients = count( $aClients ) ;
            
            $aResult = array(  
                'datacount'  => $nCountClients ,
                'datas'      => $aClients
            ) ;
            
            $strResult = Zend_Json::encode( $aResult ) ;
            echo $strResult ;
        }
        catch( Zend_Exception $e )
        {
            //write the error info to db
            $this->writeErrLog( $e->getMessage() , get_class($this) , __FUNCTION__ , print_r( $e , true ) ) ;
        }
    }   
    
    public function getlistAction()
    {
        $objClientTable = new Cts_Db_Client() ;
        $aParams = $this->getRequest()->getParams() ;
        $aResult = array() ;
        
        try
        {   
            $aLocator = array(
                'search_name' => $aParams['query']
            );
            
            $strLocator = $objClientTable->analyseLocator( $aLocator ) ;

            $aClients = $objClientTable->getList( $strLocator , NULL , NULL ) ;
            
            $nClientsCount = count( $aClients ) ;
            
            $aResult = array(
                'datacount' => $nClientsCount ,
                'datas' => $aClients
            );
            
            $strResult = Zend_Json::encode( $aResult ) ;
            echo $strResult ;
        }
        catch( Zend_Exception $e )
        {
            //write the error info to db
            $this->writeErrLog( $e->getMessage() , get_class($this) , __FUNCTION__ , print_r( $e , true ) ) ;
        }
    }
    
    public function addAction()
    {
        $objLoginTable = new Cts_Db_Login() ;
        $aParams = $this->getRequest()->getParams() ;
        $strUserName = trim( $aParams['strUserName'] ) ;
        $strLoginName = trim( strtolower( $aParams['strLogin'] ) ) ;
        $strPassword = trim( $aParams['strPassword'] ) ;
        $nUserType = intval( $aParams['user_type_id'] ) ;
        $nDepartmentId = intval( $aParams['department_id'] ) ;
        $nPositionId = intval( $aParams['position_id'] ) ;
        $nLeaderShipId = intval( $aParams['leadership_id'] ) ;
        $strTelephone = trim( $aParams['telephone'] ) ;
        $strMobile = trim( $aParams['mobile'] ) ;    
        $strEmail = trim( $aParams['email'] ) ;
        $nUserStatus = 1 ;
        
        $aInsertData = array(
            'user_type'      => $nUserType ,
            'login'          => $strLoginName ,
            'password'       => md5( $strPassword ) ,
            'name'           => $strUserName ,
            'department_id'  => $nDepartmentId ,
            'position_id'    => $nPositionId ,
            'leadership_id'  => $nLeaderShipId ,
            'telephone'      => $strTelephone ,
            'mobile'         => $strMobile ,
            'email'          => $strEmail ,
            'user_status'    => $nUserStatus 
        );
        try
        {
            $aLocator = array(
                'locate_login' => $strLoginName
            );
            $strLocator = $objLoginTable->analyseLocator( $aLocator ) ;            
            $nUserCount = $objLoginTable->getCount( $strLocator ) ;
            if( $nUserCount > 0 )
            {
                $strResult = '用户名'. $strLoginName .'已经存在' ;
                Cts_Json::echoResult( USER_IS_ALREADER_HAVE , $strResult ) ;
                return ;           
            }
            else
            {
                $nResult = $objLoginTable->insert( $aInsertData ) ;  
                if( $nResult > 0 )
                {
                    $strResult = '添加用户'. $strLoginName . '成功' ;
                    Cts_Json::echoResult( USER_ADD_SUCCESSFUL , $strResult ) ;
                    $this->writeOpLog( '成功' , __CLASS__ , __FUNCTION__) ;
                    return ;
                }      
                else
                {
                    $strResult = '添加用户'. $strLoginName . '失败' ;
                    Cts_Json::echoResult( USER_ADD_FAILD , $strResult ) ;
                    //Zend_Debug::dump( $nResult );
                    throw new Zend_Exception( '将用户'. $strLoginName . '插入数据库失败' ) ;
                    return ;
                }
                
            }
        }
        catch( Zend_Exception $e )
        {
            //write the error info to db
            $this->writeErrLog( $e->getMessage() , get_class($this) , __FUNCTION__ , print_r( $e , true ) ) ;
            return ;
        }
    }
    
    public function updateAction()
    {
        $objLoginTable = new Cts_Db_Login() ;
        $aParams = $this->getRequest()->getParams() ;
        $strUserName = trim( $aParams['strUserName'] ) ;
        $strLoginName = trim( strtolower( $aParams['strLogin'] ) ) ;
        $strPassword = trim( $aParams['strPassword'] ) ;
        $nUserType = intval( $aParams['user_type_id'] ) ;
        $nDepartmentId = intval( $aParams['department_id'] ) ;
        $nPositionId = intval( $aParams['position_id'] ) ;
        $nLeaderShipId = intval( $aParams['leadership_id'] ) ;
        $strTelephone = trim( $aParams['telephone'] ) ;
        $strMobile = trim( $aParams['mobile'] ) ;    
        $strEmail = trim( $aParams['email'] ) ;
        
        $aUpdateData = array(
            'user_type'      => $nUserType ,
            'password'       => md5( $strPassword ) ,
            'name'           => $strUserName ,
            'department_id'  => $nDepartmentId ,
            'position_id'    => $nPositionId ,
            'leadership_id'  => $nLeaderShipId ,
            'telephone'      => $strTelephone ,
            'mobile'         => $strMobile ,
            'email'          => $strEmail 
        );
        try
        {
            $aLocator = array(
                'locate_login' => $strLoginName
            );
            $strLocator = $objLoginTable->analyseLocator( $aLocator ) ;  
            $nResult = $objLoginTable->update( $aUpdateData , $strLocator ) ;  
            if( $nResult > 0 )
            {
                $strResult = '更新用户'. $strLoginName . '成功' ;
                Cts_Json::echoResult( USER_UPDATE_SUCCESSFUL , $strResult ) ;
                $this->writeOpLog( '成功' , __CLASS__ , __FUNCTION__) ;
                return ;
            }      
            else
            {
                $strResult = '更新用户'. $strLoginName . '失败' ;
                Cts_Json::echoResult( USER_UPDATE_FAILD , $strResult ) ;
                //Zend_Debug::dump( $nResult );
                throw new Zend_Exception( '将用户'. $strLoginName . '更新至数据库失败' ) ;
                return ;
            }
        }
        catch( Zend_Exception $e )
        {
            //write the error info to db
            $this->writeErrLog( $e->getMessage() , get_class($this) , __FUNCTION__ , print_r( $e , true ) ) ;
            return ;
        }
    }
    
    public function deleteAction()
    {
        $objLoginTable = new Cts_Db_Login() ;
        $aParams = $this->getRequest()->getParams() ;
        $strLoginId = trim( $aParams['nID'] ) ;
        $aLoginIds = explode( ',' , $strLoginId ) ;
        $bResult = false ;
        try
        {
            if( !empty( $aLoginIds ) )
            {
                $nCountLogins = count( $aLoginIds );
                for( $nIndexI = 0 ; $nIndexI < $nCountLogins ; $nIndexI++ )
                {
                    $aLocator = array(
                        'locate_id' => $aLoginIds[$nIndexI]
                    );
                    $strLocator = $objLoginTable->analyseLocator( $aLocator ) ; 
                    if( $objLoginTable->delete( $strLocator ) )
                    {
                        $bResult = true ;
                        
                    }      
                    else
                    {
                        $bResult = false ;
                        break ;  
                    }
                    
                }
                
                if( $bResult )
                {
                    $strResult = '删除所选择的用户成功' ;
                    Cts_Json::echoResult( USER_DELETE_SUCCESSFUL , $strResult ) ;
                    $this->writeOpLog( '成功' , __CLASS__ , __FUNCTION__) ;
                    return ;   
                } 
                else
                {
                    $strResult = '删除所选择的用户失败' ;
                    Cts_Json::echoResult( USER_DELETE_FAILD , $strResult ) ;
                    throw new Zend_Exception( '删除ID为'. $aLoginIds[$nIndexI] . '的用户失败' ) ;
                    return ;   
                }
            }
            else
            {
                $strResult = '删除用户对应参数不正确，删除用户为空。' ;
                Cts_Json::echoResult( USER_PARAMS_INVALIDED , $strResult ) ;
                throw new Zend_Exception( $strResult ) ;
                return ;
            }
        }
        catch( Zend_Exception $e )
        {
            //write the error info to db
            $this->writeErrLog( $e->getMessage() , get_class($this) , __FUNCTION__ , print_r( $e , true ) ) ;
            return ;
        }
    }
    
    public function changestatusAction()
    {
        $objLoginTable = new Cts_Db_Login() ;
        $aParams = $this->getRequest()->getParams() ;
        $strLoginId = trim( $aParams['nID'] ) ;
        $nStatus = intval( $aParams['nAction'] ) ;
        $aLoginIds = explode( ',' , $strLoginId ) ;
        $bResult = false ;
        try
        {
            if( !empty( $aLoginIds ) )
            {
                $aLocator = array(
                    'inrange_id' => $aLoginIds
                );
                $strLocator = $objLoginTable->analyseLocator( $aLocator ) ; 
                //print_r($strLocator);
                $aUpdateData = array(
                    'user_status' => $nStatus 
                );
                if( $objLoginTable->update( $aUpdateData , $strLocator ) )
                {
                    $bResult = true ; 
                }      
                else
                {
                    $bResult = false ;
                }
                                
                if( $bResult )
                {
                    $strResult = '更新所选择的用户状态成功' ;
                    Cts_Json::echoResult( USER_CHANGE_STATUS_SUCCESSFUL , $strResult ) ;
                    $this->writeOpLog( '成功' , __CLASS__ , __FUNCTION__) ;
                    return ;   
                } 
                else
                {
                    $strResult = '更新所选择的用户状态失败' ;
                    Cts_Json::echoResult( USER_CHANGE_STATUS_FAILD , $strResult ) ;
                    throw new Zend_Exception( $strResult ) ;
                    return ;   
                }
            }
            else
            {
                $strResult = '更新用户状态操作对应参数不正确，更新状态的用户为空。' ;
                Cts_Json::echoResult( USER_PARAMS_INVALIDED , $strResult ) ;
                throw new Zend_Exception( $strResult ) ;
                return ;
            }
        }
        catch( Zend_Exception $e )
        {
            //write the error info to db
            $this->writeErrLog( $e->getMessage() , get_class($this) , __FUNCTION__ , print_r( $e , true ) ) ;
            return ;
        }    
    }
}